package control;



import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.member.MemberService;
import model.member.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MemberController extends MultiActionController{
	private MemberService memberService;
	
	
	public MemberController(){}
	
	public MemberController(MemberService service) {
		super();
		this.memberService = service;
	}
	
	public ModelAndView registerForm(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("member_register_form.member");
		
	}

	
	public ModelAndView register(HttpServletRequest request,
			HttpServletResponse response,MemberVO mvo) {
		//System.out.println(vo);
		try {
				memberService.registerMember(mvo);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return new ModelAndView("home");
		
	}
	public ModelAndView loginForm(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("member_login.member");
	}
	
	public ModelAndView loginOk(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("login_left.member");
	}
	
	
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response, MemberVO mvo){
		String path="home";
		System.out.println(mvo);
		HttpSession session=request.getSession();
		try {
			MemberVO rvo = memberService.login(mvo);
			if(rvo!=null){
				session.setAttribute("mvo", rvo);
				System.out.println("로그인 성공!"+rvo);
			}else{
				path="member/login_fail";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return new ModelAndView(path);
	}
	
	
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		if(session!=null)
			session.invalidate();
		return new ModelAndView("home");
	}
	
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		return new ModelAndView("member_update.member");
	}
	
	public ModelAndView updateMember(HttpServletRequest request,
			HttpServletResponse response,MemberVO mvo){
			try {
				memberService.updateMember(mvo);
				request.getSession().setAttribute("mvo", mvo);	
				System.out.println(mvo);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			
		return new ModelAndView("member_update.member");
	}
	
	public ModelAndView deleteMember(HttpServletRequest request,
			HttpServletResponse response){
		String id=request.getParameter("id");
		HttpSession session=request.getSession(false);
		MemberVO mvo = (MemberVO)session.getAttribute("mvo");
			try {
				memberService.deleteMember(id);
				if(mvo.getUserLevel()!=0){
					session.invalidate();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return new ModelAndView("home");
	}

	public ModelAndView idCheck(HttpServletRequest request,HttpServletResponse response){
		String id=request.getParameter("id");
		System.out.println(id);
		boolean flag=false;
		try {
			flag = memberService.idCheck(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView", "flag", flag);
	}
	
	public ModelAndView findPassword(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("find_pass.member");
	}
	
	public ModelAndView findId(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
		String id=null;
		try {
			id = memberService.findId(mvo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView", "id", id);
	}
	
	public ModelAndView showHint(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
		int hint=0;
		try {
			hint = memberService.showHint(mvo);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NullPointerException e){
			hint=0;
		}
		
		return new ModelAndView("JsonView", "hint", hint);
	}
	public ModelAndView getPass(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
		String password=null;
		try {
			password=memberService.getPass(mvo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView", "password", password);
	}
	

	public ModelAndView company(HttpServletRequest request,	HttpServletResponse response){
		return new ModelAndView("company.member");
	}
	
	public ModelAndView policy(HttpServletRequest request,	HttpServletResponse response){
		return new ModelAndView("policy.member");
	}
	
	public ModelAndView limit(HttpServletRequest request,	HttpServletResponse response){
		return new ModelAndView("limit.member");
	}
	
	public ModelAndView info(HttpServletRequest request,	HttpServletResponse response){
		return new ModelAndView("info.member");
	}
	
	public ModelAndView person_join(HttpServletRequest request,	HttpServletResponse response){
		return new ModelAndView("person_join.member");
	}
	
	
	
	public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("home");
	}
}
